<template>

  <div class="common-layout">
    <el-container>
      <el-header>
        <span>{{user.userMobile}}({{ roleNames }})</span>
      </el-header>
      <el-container>

        <!-- 左边栏菜单 -->
        <el-aside width="200px">
          <el-menu
              class="el-menu-vertical-demo"
              default-active="2"
              active-text-color="#ffd04b"
              background-color="#545c64"
              text-color="#fff"
              @open="handleOpen"
              @close="handleClose"
              @select="handleLeftSelect"
            >
              <el-sub-menu :index="item.id" v-for="item in menuList" :key="item.id">
                <template #title>
                  <el-icon><location /></el-icon>
                  <span>{{item.name}}</span>
                </template>
                <el-menu-item v-for="it in item.children" :index="it.path" :key="it.id">{{it.name}}</el-menu-item>
              </el-sub-menu>

            </el-menu>
        </el-aside>

        <!-- 右侧主页面 -->
        <el-main>
          <router-view/>
        </el-main>

      </el-container>
    </el-container>
  </div>

</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router';
import { getMenuTree } from '@/api/api'


const router = useRouter();

const activeIndex2 = ref('1')
const user = ref({
  userMobile: '',
  roleList: [],
})
const roleNames = ref('');
const menuList = ref([]);

const handleSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

// 左侧菜单的点击事件
const handleLeftSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
  router.push(key);
}

const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

// 页面加载钩子函数
onMounted(() => {
  // 显示用户信息(角色)
  let u = sessionStorage.getItem('user');
  if(u == null || u == '') {
    router.push('/login');
    return
  }
  user.value = JSON.parse(u);
  roleNames.value = user.value.roleList.join(',');

  // 获取菜单树
  getMenuTree().then(res => {
    menuList.value = res.data.data;
  })
}) 
</script>

<style scoped>

</style>